package com.sprouting.ops.service;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.ops.dao.DromaraFileStorageDao;
import com.sprouting.ops.dao.OpsDeployDao;
import com.sprouting.ops.dao.OpsDeployLinkDao;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsDeploy;
import com.sprouting.ops.model.OpsDeployLink;
import com.sprouting.ops.model.OpsProductDepend;
import com.sprouting.ops.model.qo.OpsDeployQo;
import com.sprouting.ops.model.vo.DeployPortVo;
import com.sprouting.ops.model.vo.OpsDeployVo;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 部署信息表-组件和组件;(OpsDeploy)(OPS_DEPLOY) 表服务实现类
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-06-29 22:18:44
 */
@Service
@Slf4j
public class OpsDeployService extends ServiceImpl<OpsDeployDao, OpsDeploy> {

    @Resource
    private OpsDeployDao opsDeployDao;
    @Resource
    private OpsDeployLinkDao opsDeployLinkDao;
    @Resource
    private DromaraFileStorageDao dromaraFileStorageDao;

    /**
     * 分页查询 部署信息表-组件和组件;
     * @param page 分页参数
     * @param query 查询参数
     * @return
     */
    public Page<OpsDeployVo> getListPage(Page page, OpsDeployQo query){
        return opsDeployDao.getListPage(page, query);
    }

    /**
     * 根据主键删除部署信息
     * @param id 部署id
     * @param bindList 图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void delOpsDeploy(String id, List<DromaraFileStorage> bindList) {
        opsDeployDao.deleteById(id);

        // 删除关联
        LambdaUpdateWrapper<OpsDeployLink> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OpsDeployLink::getOpsDeployId, id);
        opsDeployLinkDao.delete(wrapper);

        // 删除图片
        if (CollUtil.isNotEmpty(bindList)){
            for (DromaraFileStorage dromaraFileStorage : bindList) {
                dromaraFileStorage.setTempFile(ConstantBase.TempFile.PRODUCTION_DEL);
                dromaraFileStorage.setDelTime(new Date());
                dromaraFileStorageDao.updateById(dromaraFileStorage);
            }
        }
    }

    /**
     * 保存部署信息表
     * @param opsDeploy 部署信息
     * @param deployLink 关联信息
     * @param dromaraFileStorageList Markdown的图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOpsDeploy(OpsDeploy opsDeploy, OpsDeployLink deployLink, List<DromaraFileStorage> dromaraFileStorageList) {
        opsDeployDao.insert(opsDeploy);
        opsDeployLinkDao.insert(deployLink);
        if (CollUtil.isNotEmpty(dromaraFileStorageList)){
            for (DromaraFileStorage dromaraFileStorage : dromaraFileStorageList) {
                dromaraFileStorageDao.updateById(dromaraFileStorage);
            }
        }
    }

    /**
     * 更新部署信息表-组件和组件
     * @param opsDeploy 部署信息
     * @param list Markdown的图片信息
     */
    public void updOpsDeploy(OpsDeploy opsDeploy, List<DromaraFileStorage> list) {
        opsDeployDao.updateById(opsDeploy);
        if (CollUtil.isNotEmpty(list)){
            for (DromaraFileStorage dromaraFileStorage : list) {
                dromaraFileStorageDao.updateById(dromaraFileStorage);
            }
        }
    }

    /**
     * 根据计算机id查询应用
     * @param computerServerId 计算机id
     * @return
     */
    public List<DeployPortVo> getAppByComputerServerId(String computerServerId) {
        return opsDeployDao.getAppByComputerServerId(computerServerId);
    }

    /**
     * 根据计算机id查询组件
     * @param computerServerId 计算机id
     * @return
     */
    public List<DeployPortVo> getComponentByComputerServerId(String computerServerId) {
        return opsDeployDao.getComponentByComputerServerId(computerServerId);
    }
}
